<?php
class SsApp extends MemberbaseApp {
	var $_order_mod;
	var $_ss_mod;
    function  __construct() {
        $this->ColorcardApp();
    }
    function ColorcardApp() {
    	parent::__construct();
    	$this->_order_mod=&m('order');
    	$this->_ss_mod=&m('single_system');
		$this->assign("ss_cq_type_json",ecm_json_encode(lang::get("ss_cq_type")));
    }
    //色卡列表及查询
    function index() {
    	$ss_id = $_REQUEST['ss_id']?intval($_REQUEST['ss_id']):'0';
    	if(!$ss_id){
    		$this->show_warning('错误，查看制单出错','返回订单列表','index.php?app=order');
    		return;
    	}
    	$ss_info = $this->_ss_mod->get_info($ss_id);
    	$this->assign('ss_info',$ss_info);
    	$order=$this->_order_mod->get($ss_info['order_id']);
    	if(!$order) {
    		$this->show_warning('制单不存在');
    		return;
    	}
    	if ($order['brand_id']){
    		$this->_assign_main_label($order['brand_id']);
    		$this->_assign_listing($order['brand_id']);
    	}else{
    		$this->_assign_main_label();
    		$this->_assign_listing();
    	}
		$order['company_name']=$this->getCompanyName($order['brand_id']);
		$user_info=$this->visitor->get();
		$this->assign('user',$user_info);
		
		if ($user_info['role']==USER_TAIL_MANAGE){
			/* 批改意见 */
			$file_mod=&m('upload');
			$this->assign('file_pigai', $file_mod->find(array('conditions'=>'file_type = '.BELONGS_PIGAI.' AND key_id = '.$ss_info['order_id'],'order'=>'file_id ASC')));
		}
		
		if($user_info['role']==USER_FACTORY&&$user_info['factory_id']!=$order['factory_id'])
		{
			$this->show_warning('无权限查看此制单','返回订单列表',"index.php?app=order");
    		return;
		}
		
    	$this->assign('order',$order);
    	$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
		'conditions'=>'ss_id='.$ss_id,
		'fields'=>"fabric_info.fabric_info_id,ss_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
		'join'	=>"belongs_fabric_shop",
		'order'=>'fabric_info_id ASC'));
		$this->assign('fabric_info_list', $fabric_info_list);
    	$cq_mod =& m('cq');
    	$cq_info = $cq_mod->find(array("conditions"=>"ss_id=".$ss_id));
    	$this->assign('cq_info',$cq_info);
    	$size_ss_mod =& m('size_ss');
    	$size_ss_info = $size_ss_mod->find(array("conditions"=>"ss_id=".$ss_id,'order'=>'sort_order asc'));
		$this->_filter_size($size_ss_info);
    	$this->assign('size_ss_info',$size_ss_info);
    	
    	//挂衣绳位置列表
    	$sling_mod=&m('sling');
    	$sling_list=$sling_mod->find(array(
    			'conditions'=>'if_show=1',
    			'order'=>'sling_id desc'
    	));
    	$this->assign('sling_list',$sling_list);
    	 
    	//备用扣位置列表
    	$spare_mod=&m('spare');
    	$spare_list=$spare_mod->find(array(
    			'conditions'=>'if_show=1',
    			'order'=>'spare_id desc'
    	));
    	$this->assign('spare_list',$spare_list);
    	 
    	//洗水唛位置列表
    	$water_mark_mod=&m('water_mark');
    	$water_list=$water_mark_mod->find(array(
    			'conditions'=>'if_show=1',
    			'order'=>'water_id desc'
    	));
    	$this->assign('water_list',$water_list);
    	
    	//洗水唛附件
    	$file_mod=&m('upload');
    	$ss_waters=$file_mod->find(array(
    			'conditions'=>'file_type='.BELONGS_SS_WATER_IMG.' and key_id='.$ss_id,
    			'order'=>'file_id desc'
    	));
    	$this->assign('ss_waters',$ss_waters);
    	
    	//包装要求位置列表
    	$package_mod=&m('package_demand');
    	if ($ss_info['pack_id']){
    		$pack_arr_id=explode(',', $ss_info['pack_id']);
    		$con=" and pack_id not ".db_create_in($pack_arr_id);
    		$pack_list=$package_mod->find(array(
    				'conditions'=>'if_show=1 and pack_id '.db_create_in($pack_arr_id),
    				'order'=>'pack_id desc'
    		));
    		$this->assign('pack_list',$pack_list);
    	}
    	$demand_list=$package_mod->find(array(
    			'conditions'=>'if_show=1'.$con,
    			'order'=>'pack_id desc'
    	));
    	$this->assign('demand_list',$demand_list);
    	
		//赋值颜色数量类型
		$ss_cq_type=LANG::get("ss_cq_type");
		$this->_assign_sizename($this,$ss_info['cq_sn_id'],$ss_info['size_sn_id']);
		//赋值尺寸表类型
		$this->assign("curr_size_type",$ss_cq_type[$ss_info['size_type']]);
		if($this->visitor->get('role')==USER_MERCHANDISER){
			$this->assign('is_edit',true);
		}
		$this->display('ss.index.html');
    }

    //
    function add() {
    	$id=$_GET['id']?intval($_GET['id']):0;
    	if(!$id) {
    		$this->show_warning('请选择制单');
    		return;
    	}
		$style_mod =& m('style');
    	$ss_info = $this->_ss_mod->get("order_id=".$id);
	
    	if($ss_info){
			header('location:index.php?app=ss&act=edit&ss_id='.$ss_info['ss_id']);
    		return;
    	}

		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
		'conditions'=>'order_id='.$id,
		'fields'=>"fabric_info.fabric_info_id,ss_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
		'join'	=>"belongs_fabric_shop",
		'order'=>'fabric_info_id ASC'));
		$this->assign('fabric_info_list', $fabric_info_list);
    	if(!IS_POST) {
	    	$order=$this->_order_mod->get($id);
	    	if(!$order) {
	    		$this->show_warning('制单不存在');
	    		return;
	    	}
			
	    	if ($order['brand_id']){
	    		$this->_assign_main_label($order['brand_id']);
	    		$this->_assign_listing($order['brand_id']);
	    	}else{
	    		$this->_assign_main_label();
	    		$this->_assign_listing();
	    	}
			
			$order['company_name']=$this->getCompanyName($order['brand_id']);
	    	$colorcard_mode=& m('color_card');
	    	$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");
	    	$this->assign("fabric_shop_list",$fabric_shop_list);
	    	$this->assign('order',$order);
	    	$size_mod =& m('size');
			$size_ss_info=$size_mod->find(array('conditions'=>'style_id='.$order['style_id'],'order'=>'sort_order ASC'));
			$this->_filter_size($size_ss_info);
	    	$this->assign('size_ss_info',$size_ss_info);
			
			$style_info=$style_mod ->get($order['style_id']);
			
			//挂衣绳位置列表
			$sling_mod=&m('sling');
			$sling_list=$sling_mod->find(array(
				'conditions'=>'if_show=1',
				'order'=>'sling_id desc'	
			));
			$this->assign('sling_list',$sling_list);
			
			//备用扣位置列表
			$spare_mod=&m('spare');
			$spare_list=$spare_mod->find(array(
					'conditions'=>'if_show=1',
					'order'=>'spare_id desc'
			));
			$this->assign('spare_list',$spare_list);
			
			//洗水唛位置列表
			$water_mark_mod=&m('water_mark');
			$water_list=$water_mark_mod->find(array(
					'conditions'=>'if_show=1',
					'order'=>'water_id desc'
			));
			
			//包装要求位置列表
			$package_mod=&m('package_demand');
			$demand_list=$package_mod->find(array(
					'conditions'=>'if_show=1',
					'order'=>'pack_id desc'
			));
			$this->assign('demand_list',$demand_list);
			
			if($this->visitor->get('role')==USER_MERCHANDISER){
				$this->assign('is_edit',true);
			}
			
			//赋值尺寸表类型
			$this->_assign_sizename($this,NULL,$style_info['style_size_sn_id']);
			$this->assign('ss_info',array('size_sn_id'=>$style_info['style_size_sn_id'],'size_select'=>array("1","1","1","1","1","1")));
			$this->assign('belongs_ss_water',BELONGS_SS_WATER_IMG);
			$this->assign('default_key_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->display('ss.edit.html');
    	}else{
    		//print_r($_POST);exit;
			$size_checked_v1=isset($_POST['size_checked_v1'])?intval($_POST['size_checked_v1']):0;
			$size_checked_v2=isset($_POST['size_checked_v2'])?intval($_POST['size_checked_v2']):0;
			$size_checked_v3=isset($_POST['size_checked_v3'])?intval($_POST['size_checked_v3']):0;
			$size_checked_v4=isset($_POST['size_checked_v4'])?intval($_POST['size_checked_v4']):0;
			$size_checked_v5=isset($_POST['size_checked_v5'])?intval($_POST['size_checked_v5']):0;
			$size_checked_v6=isset($_POST['size_checked_v6'])?intval($_POST['size_checked_v6']):0;
			$is_sling=isset($_POST['is_sling'])?$_POST['is_sling']:'';
			$is_spare=isset($_POST['is_spare'])?$_POST['is_spare']:'';
    		//制单表数据
    		$ss_data=array(
    				"order_id"=>$id,
    				"PGR"=>isset($_POST['PGR'])?trim($_POST['PGR']):"",
    				"SGR"=>isset($_POST['SGR'])?trim($_POST['SGR']):"",
    				"ITEM_GROUP"=>isset($_POST['ITEM_GROUP'])?trim($_POST['ITEM_GROUP']):"",
    				"SEASON"=>isset($_POST['SEASON'])?trim($_POST['SEASON']):"",
    				"opinion"=>isset($_POST['opinion'])?trim($_POST['opinion']):"",
					"maitou"=>isset($_POST['maitou'])?trim($_POST['maitou']):"",
					"chegnfen"=>isset($_POST['chegnfen'])?trim($_POST['chegnfen']):"",
    				"drops"=>isset($_POST['drops'])?trim($_POST['drops']):"",
					"ss_status"=>SS_STATUS_CREATE,
					"cq_sn_id"=>isset($_POST['cq_sn_id'])?intval($_POST['cq_sn_id']):0,
					"size_sn_id"=>isset($_POST['size_sn_id'])?intval($_POST['size_sn_id']):0,
					"color_requir"=>isset($_POST['color_requir'])?trim($_POST['color_requir']):'',
					"size_select"=>"{$size_checked_v1},{$size_checked_v2},{$size_checked_v3},{$size_checked_v4},{$size_checked_v5},{$size_checked_v6}",
					"ss_bow"=>isset($_POST['ss_bow'])?trim($_POST['ss_bow']):""
    		);
    		$pack_id=isset($_POST['sub_demand'])?$_POST['sub_demand']:0;
    		$sling_id=0;
    		$sling_length=0;
    		if ($is_sling){
    			$sling_id=isset($_POST['sling_id'])?$_POST['sling_id']:'';
    			$sling_length=isset($_POST['sling_length'])?$_POST['sling_length']:'';
    		}
    		$spare_id=0;
    		if ($is_spare){
    			$spare_id=isset($_POST['spare_id'])?$_POST['spare_id']:'';
    		}
    		$pack_arr_id=0;
    		if($pack_id){
    			$pack_arr_id=implode(',', $pack_id);
    		}
    		$m_id=isset($_POST['main_label'])?intval($_POST['main_label']):0;
    		$list_id=isset($_POST['listing_code'])?intval($_POST['listing_code']):0;
    		$bead_list_id=isset($_POST['bead_listing_code'])?intval($_POST['bead_listing_code']):0;
    		$water_id=isset($_POST['water_id'])?intval($_POST['water_id']):0;
    		$ss_data['sling_id']=$sling_id;
    		$ss_data['sling_length']=$sling_length;
    		$ss_data['spare_id']=$spare_id;
    		$ss_data['ss_m_id']=$m_id;
    		$ss_data['ss_list_id']=$list_id;
    		$ss_data['bead_list_id']=$bead_list_id;
    		$ss_data['water_id']=$water_id;
    		$ss_data['pack_id']=$pack_arr_id;
    		$ss_id = $this->_ss_mod->add($ss_data);
    		if(!$ss_id) {
    			$this->show_warning('制单不成功');
    			return;
    		}
    		//颜色shu数量数据
    		foreach($_POST['color'] as $key=>$item)
    		{
    			if($_POST['color'][$key]){
	    			$cq_data = array(
	    				"ss_id" =>$ss_id,
	    				"color" =>isset($_POST['color'][$key])?trim($_POST['color'][$key]):"",
						"color_desc" =>isset($_POST['color_desc'][$key])?trim($_POST['color_desc'][$key]):"",
						"color_num" =>isset($_POST['color_num'][$key])?trim($_POST['color_num'][$key]):"",
	    				"proportion" =>isset($_POST['proportion'][$key])?trim($_POST['proportion'][$key]):"",
	    				"v6" =>isset($_POST['v6'][$key])?intval($_POST['v6'][$key]):"0",
	    				"v8" =>isset($_POST['v8'][$key])?intval($_POST['v8'][$key]):"0",
	    				"v10" =>isset($_POST['v10'][$key])?intval($_POST['v10'][$key]):"0",
	    				"v12" =>isset($_POST['v12'][$key])?intval($_POST['v12'][$key]):"0",
	    				"v14" =>isset($_POST['v14'][$key])?intval($_POST['v14'][$key]):"0",
	    				"v16" =>isset($_POST['v16'][$key])?intval($_POST['v16'][$key]):"0",
	    				"total" =>isset($_POST['total'][$key])?intval($_POST['total'][$key]):"0",
	    			);
	    			$cq_mod =& m('cq');
	    			$cq_id = $cq_mod->add($cq_data);
    			}
    		}
    		//尺寸表数据
    		foreach($_POST['position'] as $key=>$item)
    		{

	    			$size_data = array(
	    					"ss_id" =>$ss_id,
	    					"position" =>isset($_POST['position'][$key])?trim($_POST['position'][$key]):"",
    					"w6" =>isset($_POST['w6'][$key])&&!empty($_POST['w6'][$key])?floatval($_POST['w6'][$key]):NULL,
    					"w8" =>isset($_POST['w8'][$key])&&!empty($_POST['w8'][$key])?floatval($_POST['w8'][$key]):NULL,
    					"w10" =>isset($_POST['w10'][$key])&&!empty($_POST['w10'][$key])?floatval($_POST['w10'][$key]):NULL,
    					"w12" =>isset($_POST['w12'][$key])&&!empty($_POST['w12'][$key])?floatval($_POST['w12'][$key]):NULL,
    					"w14" =>isset($_POST['w14'][$key])&&!empty($_POST['w14'][$key])?floatval($_POST['w14'][$key]):NULL,
    					"w16" =>isset($_POST['w16'][$key])&&!empty($_POST['w16'][$key])?floatval($_POST['w16'][$key]):NULL,
    					"code" =>isset($_POST['code'][$key])&&!empty($_POST['code'][$key])?floatval($_POST['code'][$key]):NULL,
	    				"sort_order"=>$key
	    			);
					if(!empty($size_data['position'])||!empty($size_data['w6'])||!empty($size_data['w8'])||!empty($size_data['w10'])||!empty($size_data['w12'])||!empty($size_data['w14'])||!empty($size_data['w16'])||!empty($size_data['code']))
					{
						foreach($size_data as $key=>$item)
						{
						  if(in_array($key,array("w6","w8","w10","w12","w14","w16","code")))
						  {
							  if(empty($item))
							  {
							  unset($size_data[$key]);
							  }
						  }
						}
	    			$size_ss_mod =& m('size_ss');
	    			$size_ss_id = $size_ss_mod->add($size_data);
					}
    			
    		}
			
			//更改面料信息
			$new_fabric_ids=array();

			foreach($_POST['fabric_shop_name'] as $key=>$item)
			{
				$fabric_info_id=isset($_POST['fabric_info_id'][$key])?intval($_POST['fabric_info_id'][$key]):0;
				$fabric_data=array(
					"ss_id"=>$ss_id,
					"description"=>isset($_POST['description'][$key])?trim($_POST['description'][$key]):"",
					"fabric_shop_id"=>isset($_POST['fabric_shop_name'][$key])?intval($_POST['fabric_shop_name'][$key]):"",
					"cc_id"=>isset($_POST['cc_id'][$key])?intval($_POST['cc_id'][$key]):0,
					"item"=>isset($_POST['item'][$key])?trim($_POST['item'][$key]):"",
					"price"=>isset($_POST['price'][$key])?trim($_POST['price'][$key]):"",
					"width"=>isset($_POST['width'][$key])?trim($_POST['width'][$key]):"",
					"fabric_characteristics"=>isset($_POST['fabric_characteristics'][$key])?trim($_POST['fabric_characteristics'][$key]):""
				);
				//print_r($_POST);exit();
				if(!empty($fabric_data['fabric_shop_id'])&&!empty($fabric_data['item']))
				{
				  if(empty($fabric_info_id))
				  {
					  $fabric_info_mod->add($fabric_data);
				  }
				  else
				  {
					  $new_fabric_ids[]=$fabric_info_id;

					  $fabric_info_mod->edit("fabric_info_id={$fabric_info_id}",$fabric_data);
				  }		
				}

			}

			foreach($fabric_info_list as $item)
			{
				if(!in_array($item['fabric_info_id'],$new_fabric_ids))
				{
					$fabric_info_mod->drop("fabric_info_id={$item['fabric_info_id']}");
				}
			}
			
			$ss_water_id=isset($_POST['water_img_id'])?$_POST['water_img_id']:"";
			$file_mod =& m('upload');
			if(!empty($ss_water_id))
			{
				$file_ids=implode(",",$ss_water_id);
				$cust_files_info=$file_mod->find(array(
						"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_SS_WATER_IMG." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
								));
				 
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $ss_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$ss_id,
							"session_id"=>session_id()
					));
				}
			}
			
    		if($ss_id){
    			$this->show_message('添加制单成功！',"编辑制单","index.php?app=ss&act=edit&ss_id={$ss_id}","返回订单页面","index.php?app=order&act=edit&id={$id}");
    		}
    	}
    }
    

    
    //
    function edit(){
    	$ss_id = $_REQUEST['ss_id']?intval($_REQUEST['ss_id']):'0';
    	if(!$ss_id){
    		$this->show_warning('错误，编辑出错','返回订单列表','index.php?app=order');
    		return;
    	}
		$ss_info = $this->_ss_mod->get_info($ss_id);
		
		if($ss_info['ss_status']!=SS_STATUS_CREATE)
		{
			$this->show_warning("当前制单状态不能编辑",'查看制单',"index.php?app=ss&act=index&ss_id={$ss_id}");
    		return;
		}

		$ss_info['size_select']=explode(",",$ss_info['size_select']);
		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
		'conditions'=>'ss_id='.$ss_id,
		'fields'=>"fabric_info.fabric_info_id,ss_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
		'join'	=>"belongs_fabric_shop",
		'order'=>'fabric_info_id ASC'));
		$this->assign('fabric_info_list', $fabric_info_list);
		$order=$this->_order_mod->get($ss_info['order_id']);
		if(!$order) {
			$this->show_warning('制单不存在');
			return;
		}
		
		if ($order['brand_id']){
			$this->_assign_main_label($order['brand_id']);
			$this->_assign_listing($order['brand_id']);
		}else{
			$this->_assign_main_label();
			$this->_assign_listing();
		}
		$order['company_name']=$this->getCompanyName($order['brand_id']);
		
		$cq_mod =& m('cq');
		$cq_info = $cq_mod->find(array("conditions"=>"ss_id=".$ss_id));
		$this->assign('cq_info',$cq_info);
		$size_ss_mod =& m('size_ss');
		$size_ss_info = $size_ss_mod->find(array("conditions"=>"ss_id=".$ss_id,'order'=>'sort_order ASC'));
		$this->_filter_size($size_ss_info);

		$this->assign('size_ss_info',$size_ss_info);
		
    	if(!$_POST){

    		$this->assign('ss_info',$ss_info);
    		$colorcard_mode=& m('color_card');
    		$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");
    		$this->assign("fabric_shop_list",$fabric_shop_list);
    		$this->assign('order',$order);
    		//挂衣绳位置列表
    		$sling_mod=&m('sling');
    		$sling_list=$sling_mod->find(array(
    				'conditions'=>'if_show=1',
    				'order'=>'sling_id desc'
    		));
    		$this->assign('sling_list',$sling_list);
    			
    		//备用扣位置列表
    		$spare_mod=&m('spare');
    		$spare_list=$spare_mod->find(array(
    				'conditions'=>'if_show=1',
    				'order'=>'spare_id desc'
    		));
    		$this->assign('spare_list',$spare_list);
    			
    		//洗水唛位置列表
    		$water_mark_mod=&m('water_mark');
    		$water_list=$water_mark_mod->find(array(
    				'conditions'=>'if_show=1',
    				'order'=>'water_id desc'
    		));
    		$this->assign('water_list',$water_list);
    		
    		//洗水唛附件
    		$file_mod=&m('upload');
    		$ss_waters=$file_mod->find(array(
    			'conditions'=>'file_type='.BELONGS_SS_WATER_IMG.' and key_id='.$ss_id,
    			'order'=>'file_id desc'	
    		));
    		$this->assign('ss_waters',$ss_waters);
    		
    		//包装要求位置列表
    		$package_mod=&m('package_demand');
    		if ($ss_info['pack_id']){
    			$pack_arr_id=explode(',', $ss_info['pack_id']);
    			$con=" and pack_id not ".db_create_in($pack_arr_id);
    			$pack_list=$package_mod->find(array(
    				'conditions'=>'if_show=1 and pack_id '.db_create_in($pack_arr_id),
    				'order'=>'pack_id desc'
    			));
    			$this->assign('pack_list',$pack_list);
    		}
    		$demand_list=$package_mod->find(array(
    				'conditions'=>'if_show=1'.$con,
    				'order'=>'pack_id desc'
    		));
    		$this->assign('demand_list',$demand_list);
    		
			$this->_assign_sizename($this,$ss_info['cq_sn_id'],$ss_info['size_sn_id']);
			//赋值颜色数量类型
			$ss_cq_type=LANG::get("ss_cq_type");
			//赋值尺寸表类型
			$this->assign("curr_size_type",$ss_cq_type[$ss_info['size_type']]);
			$this->assign('belongs_ss_water',BELONGS_SS_WATER_IMG);
			if($this->visitor->get('role')==USER_MERCHANDISER){
				$this->assign('is_edit',true);
			}
    		$this->display('ss.edit.html');
	    	return;
    	}else{
			$size_checked_v1=isset($_POST['size_checked_v1'])?intval($_POST['size_checked_v1']):0;
			$size_checked_v2=isset($_POST['size_checked_v2'])?intval($_POST['size_checked_v2']):0;
			$size_checked_v3=isset($_POST['size_checked_v3'])?intval($_POST['size_checked_v3']):0;
			$size_checked_v4=isset($_POST['size_checked_v4'])?intval($_POST['size_checked_v4']):0;
			$size_checked_v5=isset($_POST['size_checked_v5'])?intval($_POST['size_checked_v5']):0;
			$size_checked_v6=isset($_POST['size_checked_v6'])?intval($_POST['size_checked_v6']):0;
			$is_sling=isset($_POST['is_sling'])?$_POST['is_sling']:'';
			$is_spare=isset($_POST['is_spare'])?$_POST['is_spare']:'';
    		//制单表数据
    		$ss_data=array(
    				"PGR"=>isset($_POST['PGR'])?trim($_POST['PGR']):"",
    				"SGR"=>isset($_POST['SGR'])?trim($_POST['SGR']):"",
    				"ITEM_GROUP"=>isset($_POST['ITEM_GROUP'])?trim($_POST['ITEM_GROUP']):"",
    				"SEASON"=>isset($_POST['SEASON'])?trim($_POST['SEASON']):"",
    				"opinion"=>isset($_POST['opinion'])?trim($_POST['opinion']):"",
					"maitou"=>isset($_POST['maitou'])?trim($_POST['maitou']):"",
    				"drops"=>isset($_POST['drops'])?trim($_POST['drops']):"",
					"chegnfen"=>isset($_POST['chegnfen'])?trim($_POST['chegnfen']):"",
					"cq_sn_id"=>isset($_POST['cq_sn_id'])?intval($_POST['cq_sn_id']):0,
					"size_sn_id"=>isset($_POST['size_sn_id'])?intval($_POST['size_sn_id']):0,
					"color_requir"=>isset($_POST['color_requir'])?trim($_POST['color_requir']):'',
					"size_select"=>"{$size_checked_v1},{$size_checked_v2},{$size_checked_v3},{$size_checked_v4},{$size_checked_v5},{$size_checked_v6}",
					"ss_bow"=>isset($_POST['ss_bow'])?trim($_POST['ss_bow']):""
    		);
    		$pack_id=isset($_POST['sub_demand'])?$_POST['sub_demand']:0;
    		$sling_id=0;
    		$sling_length=0;
    		if ($is_sling){
    			$sling_id=isset($_POST['sling_id'])?$_POST['sling_id']:'';
    			$sling_length=isset($_POST['sling_length'])?$_POST['sling_length']:'';
    		}
    		$spare_id=0;
    		if ($is_spare){
    			$spare_id=isset($_POST['spare_id'])?$_POST['spare_id']:'';
    		}
    		$pack_arr_id=0;
    		if($pack_id){
    			$pack_arr_id=implode(',', $pack_id);
    		}
    		$m_id=isset($_POST['main_label'])?intval($_POST['main_label']):0;
    		$list_id=isset($_POST['listing_code'])?intval($_POST['listing_code']):0;
    		$bead_list_id=isset($_POST['bead_listing_code'])?intval($_POST['bead_listing_code']):0;
    		$water_id=isset($_POST['water_id'])?intval($_POST['water_id']):0;
    		$ss_data['sling_id']=$sling_id;
    		$ss_data['sling_length']=$sling_length;
    		$ss_data['spare_id']=$spare_id;
    		$ss_data['ss_m_id']=$m_id;
    		$ss_data['ss_list_id']=$list_id;
    		$ss_data['bead_list_id']=$bead_list_id;
    		$ss_data['water_id']=$water_id;
    		$ss_data['pack_id']=$pack_arr_id;
    		//颜色shu数量数据
    		$cq_mod =& m('cq');
			$new_cq_ids=array();
			
    		foreach($_POST['color'] as $key=>$item)
    		{
				$cq_id=isset($_POST['cq_id'][$key])?intval($_POST['cq_id'][$key]):0;
				$cq_data = array(
    					"ss_id" =>$ss_id,
    					"color" =>isset($_POST['color'][$key])?trim($_POST['color'][$key]):"",
						"color_desc" =>isset($_POST['color_desc'][$key])?trim($_POST['color_desc'][$key]):"",
						"color_num" =>isset($_POST['color_num'][$key])?trim($_POST['color_num'][$key]):"",
    					"proportion" =>isset($_POST['proportion'][$key])?trim($_POST['proportion'][$key]):"",
    					"v6" =>isset($_POST['v6'][$key])?intval($_POST['v6'][$key]):"0",
    					"v8" =>isset($_POST['v8'][$key])?intval($_POST['v8'][$key]):"0",
    					"v10" =>isset($_POST['v10'][$key])?intval($_POST['v10'][$key]):"0",
    					"v12" =>isset($_POST['v12'][$key])?intval($_POST['v12'][$key]):"0",
    					"v14" =>isset($_POST['v14'][$key])?intval($_POST['v14'][$key]):"0",
    					"v16" =>isset($_POST['v16'][$key])?intval($_POST['v16'][$key]):"0",
    					"total" =>isset($_POST['total'][$key])?intval($_POST['total'][$key]):"0",
    			);
				
				if(!empty($cq_data['color']))
				{
				  if(empty($cq_id))
				  {
					  $cq_mod->add($cq_data);
				  }
				  else
				  {
					  $new_cq_ids[]=$cq_id;

					  $cq_mod->edit("cq_id={$cq_id}",$cq_data);
				  }		
				}
    		}
			
			foreach($cq_info as $item)
			{
				if(!in_array($item['cq_id'],$new_cq_ids))
				{
					$cq_mod->drop("cq_id={$item['cq_id']}");
				}
			}
			
    		//尺寸表数据
    		$size_ss_mod =& m('size_ss');
			$new_size_ss_ids=array();
			
    		foreach($_POST['position'] as $key=>$item)
    		{
				$size_ss_id=isset($_POST['size_ss_id'][$key])?intval($_POST['size_ss_id'][$key]):0;
    		
    			$size_data = array(
    					"ss_id" =>$ss_id,
    					"position" =>isset($_POST['position'][$key])?trim($_POST['position'][$key]):"",
    					"w6" =>isset($_POST['w6'][$key])&&!empty($_POST['w6'][$key])?floatval($_POST['w6'][$key]):NULL,
    					"w8" =>isset($_POST['w8'][$key])&&!empty($_POST['w8'][$key])?floatval($_POST['w8'][$key]):NULL,
    					"w10" =>isset($_POST['w10'][$key])&&!empty($_POST['w10'][$key])?floatval($_POST['w10'][$key]):NULL,
    					"w12" =>isset($_POST['w12'][$key])&&!empty($_POST['w12'][$key])?floatval($_POST['w12'][$key]):NULL,
    					"w14" =>isset($_POST['w14'][$key])&&!empty($_POST['w14'][$key])?floatval($_POST['w14'][$key]):NULL,
    					"w16" =>isset($_POST['w16'][$key])&&!empty($_POST['w16'][$key])?floatval($_POST['w16'][$key]):NULL,
    					"code" =>isset($_POST['code'][$key])&&!empty($_POST['code'][$key])?floatval($_POST['code'][$key]):NULL,
    					"sort_order"=>$key
    			);
				
				if(!empty($size_data['position'])||!empty($size_data['w6'])||!empty($size_data['w8'])||!empty($size_data['w10'])||!empty($size_data['w12'])||!empty($size_data['w14'])||!empty($size_data['w16'])||!empty($size_data['code']))
				{	
					
					if(empty($size_ss_id))
					{
						$size_ss_mod->add($size_data);
					}
					else
					{
						$new_size_ss_ids[]=$size_ss_id;
  
						$size_ss_mod->edit("size_ss_id={$size_ss_id}",$size_data);
						
					}	
					
				}
    			
    		}

			foreach($size_ss_info as $item)
			{
				if(!in_array($item['size_ss_id'],$new_size_ss_ids))
				{
					$size_ss_mod->drop("size_ss_id={$item['size_ss_id']}");
				}
			}
			
			
			$submit_val=isset($_POST['submit'])?trim($_POST['submit']):"";
			if($submit_val=="送主管审批")
			{
				$ss_data['ss_status']=SS_STATUS_APPROVE;
			}
		
    		$this->_ss_mod->edit($ss_id,$ss_data);
			
			//更改面料信息
			$new_fabric_ids=array();

			foreach($_POST['fabric_shop_name'] as $key=>$item)
			{
				$fabric_info_id=isset($_POST['fabric_info_id'][$key])?intval($_POST['fabric_info_id'][$key]):0;
				$fabric_data=array(
					"ss_id"=>$ss_id,
					"description"=>isset($_POST['description'][$key])?trim($_POST['description'][$key]):"",
					"fabric_shop_id"=>isset($_POST['fabric_shop_name'][$key])?intval($_POST['fabric_shop_name'][$key]):"",
					"cc_id"=>isset($_POST['cc_id'][$key])?intval($_POST['cc_id'][$key]):0,
					"item"=>isset($_POST['item'][$key])?trim($_POST['item'][$key]):"",
					"price"=>isset($_POST['price'][$key])?trim($_POST['price'][$key]):"",
					"width"=>isset($_POST['width'][$key])?trim($_POST['width'][$key]):"",
					"fabric_characteristics"=>isset($_POST['fabric_characteristics'][$key])?trim($_POST['fabric_characteristics'][$key]):""
				);
				//print_r($_POST);exit();
				if(!empty($fabric_data['fabric_shop_id'])&&!empty($fabric_data['item']))
				{
				  if(empty($fabric_info_id))
				  {
					  $fabric_info_mod->add($fabric_data);
				  }
				  else
				  {
					  $new_fabric_ids[]=$fabric_info_id;

					  $fabric_info_mod->edit("fabric_info_id={$fabric_info_id}",$fabric_data);
				  }		
				}

			}

			foreach($fabric_info_list as $item)
			{
				if(!in_array($item['fabric_info_id'],$new_fabric_ids))
				{
					$fabric_info_mod->drop("fabric_info_id={$item['fabric_info_id']}");
				}
			}
			
			//当送审批时发送邮件
			$new_ss_info=$this->_ss_mod->get_info($ss_id);
			if($ss_info['ss_status']==SS_STATUS_CREATE&&$new_ss_info['ss_status']==SS_STATUS_APPROVE)  //送主管审批时发送邮件
			{
				$this->_send_mail_on_send_approve($order);
			}
			
			if($submit_val=="送主管审批")
			{
    		$this->show_message('编辑制单成功！',"查看制单","index.php?app=ss&act=index&ss_id={$ss_id}");
			}
			else
			{
				$this->show_message('编辑制单成功！',"编辑制单","index.php?app=ss&act=edit&ss_id={$ss_id}");
			}
    	}
    
    }
 
 
	 //主管审批制单
	 function approve()
	 {
		$ss_id = $_REQUEST['ss_id']?intval($_REQUEST['ss_id']):'0';
    	if(!$ss_id){
    		$this->show_warning('错误，审批出错','返回订单列表','index.php?app=order');
    		return;
    	}

		$ss_info = $this->_ss_mod->get_info($ss_id);
		$this->assign('ss_info',$ss_info);
		if($ss_info['ss_status']!=SS_STATUS_APPROVE)
		{
			header("location:index.php?app=ss&act=index&ss_id={$ss_id}");
    		return;
		}
		
		$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
		'conditions'=>'ss_id='.$ss_id,
		'fields'=>"fabric_info.fabric_info_id,ss_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
		'join'	=>"belongs_fabric_shop",
		'order'=>'fabric_info_id ASC'));
		$this->assign('fabric_info_list', $fabric_info_list);
		$order=$this->_order_mod->get($ss_info['order_id']);
		
		
    	if(!$_POST){
    		$ss_info = $this->_ss_mod->get_info($ss_id);
    		$this->assign('ss_info',$ss_info);

    		if(!$order) {
    			$this->show_warning('制单不存在');
    			return;
    		}
    		if ($order['brand_id']){
    			$this->_assign_main_label($order['brand_id']);
    			$this->_assign_listing($order['brand_id']);
    		}else{
    			$this->_assign_main_label();
    			$this->_assign_listing();
    		}
			$order['company_name']=$this->getCompanyName($order['brand_id']);
    		$colorcard_mode=& m('color_card');
    		$fabric_shop_list=$colorcard_mode->getall("select fabric_shop_name,fabric_shop_id from {$colorcard_mode->table} group by fabric_shop_id order by fabric_shop_name,update_time desc");
    		$this->assign("fabric_shop_list",$fabric_shop_list);
    		$this->assign('order',$order);
    		$cq_mod =& m('cq');
    		$cq_info = $cq_mod->find(array("conditions"=>"ss_id=".$ss_id));
    		$this->assign('cq_info',$cq_info);
    		$size_ss_mod =& m('size_ss');
			$size_ss_info = $size_ss_mod->find(array("conditions"=>"ss_id=".$ss_id,'order'=>'sort_order asc'));
			$this->_filter_size($size_ss_info);
    		$this->_assign_sizename($this,$ss_info['cq_sn_id'],$ss_info['size_sn_id']);    		
			$this->assign('size_ss_info',$size_ss_info);
			
			//挂衣绳位置列表
			$sling_mod=&m('sling');
			$sling_list=$sling_mod->find(array(
					'conditions'=>'if_show=1',
					'order'=>'sling_id desc'
			));
			$this->assign('sling_list',$sling_list);
			
			//备用扣位置列表
			$spare_mod=&m('spare');
			$spare_list=$spare_mod->find(array(
					'conditions'=>'if_show=1',
					'order'=>'spare_id desc'
			));
			$this->assign('spare_list',$spare_list);
			
			//洗水唛位置列表
			$water_mark_mod=&m('water_mark');
			$water_list=$water_mark_mod->find(array(
					'conditions'=>'if_show=1',
					'order'=>'water_id desc'
			));
			$this->assign('water_list',$water_list);
			 
			//洗水唛附件
			$file_mod=&m('upload');
			$ss_waters=$file_mod->find(array(
					'conditions'=>'file_type='.BELONGS_SS_WATER_IMG.' and key_id='.$ss_id,
					'order'=>'file_id desc'
			));
			$this->assign('ss_waters',$ss_waters);
			
			if($this->visitor->get('role')==USER_MERCHANDISER){
				$this->assign('is_edit',true);
			}
			
			//包装要求位置列表
			$package_mod=&m('package_demand');
			if ($ss_info['pack_id']){
				$pack_arr_id=explode(',', $ss_info['pack_id']);
				$con=" and pack_id not ".db_create_in($pack_arr_id);
				$pack_list=$package_mod->find(array(
						'conditions'=>'if_show=1 and pack_id '.db_create_in($pack_arr_id),
						'order'=>'pack_id desc'
				));
				$this->assign('pack_list',$pack_list);
			}
			$demand_list=$package_mod->find(array(
					'conditions'=>'if_show=1'.$con,
					'order'=>'pack_id desc'
			));
			$this->assign('demand_list',$demand_list);
			$this->assign('belongs_ss_water',BELONGS_SS_WATER_IMG);
    		$this->display('ss.index.html');
	    	return;
    	}else{
    		
		$submit_val=isset($_POST['submit'])?trim($_POST['submit']):"";
		$approve_no_desc=isset($_POST['approve_no_desc'])?trim($_POST['approve_no_desc']):"";
			
		$ss_data=array();
		if($submit_val=="审批通过")
		{
			$ss_data['ss_status']=SS_STATUS_SUCCESS;
		}
		elseif($submit_val=="审批不通过")
		{
			$ss_data['ss_status']=SS_STATUS_CREATE;
			if (empty($approve_no_desc)) {
				$this->show_warning('请输入审批不通过理由');
				return;
			 }
		}
		
    	$this->_ss_mod->edit($ss_id,$ss_data);
		if($this->_ss_mod->has_error())
		{
			$this->show_warning($this->_ss_mod->get_error());
    		return;
		}
		
		
		//当审批通过和不通过时发送邮件
			$mod_pro_records=&m("pro_records");
			$log_time=gmtime();
		 $new_ss_info=$this->_ss_mod->get_info($ss_id);
		  if($ss_info['ss_status']==SS_STATUS_APPROVE&&$new_ss_info['ss_status']==SS_STATUS_CREATE)  //审批不通过时发送邮件
		  {
			  $order["approve_no_desc"]=$approve_no_desc;
			  $pro_data=array(
					"type"=>PRO_RECORDS_ORDER,
					"key_id"=>$order["order_id"],
					"rec_name"=>$this->visitor->get("user_name"),
					"rec_buff"=>"制单审批不通过,理由:{$approve_no_desc}",
					"rec_time"=>$log_time			
				);
				if(!$mod_pro_records->add($pro_data))
				{
				   $this->show_warning($mod_pro_records->get_error());
				}
			  $this->_send_mail_on_approve_not_pass($order);
		  }
		  elseif($ss_info['ss_status']==SS_STATUS_APPROVE&&$new_ss_info['ss_status']==SS_STATUS_SUCCESS)  //审批通过时发送邮件
		  {
			  	$pro_data=array(
					"type"=>PRO_RECORDS_ORDER,
					"key_id"=>$order["order_id"],
					"rec_name"=>$this->visitor->get("user_name"),
					"rec_buff"=>"已审批制单",
					"rec_time"=>$log_time			
				);
				if(!$mod_pro_records->add($pro_data))
				{
				   $this->show_warning($mod_pro_records->get_error());
				}
			  $this->_send_mail_on_approve_pass($order);
		  }
		  
    		$this->show_message('审批制单成功！',"编辑制单","index.php?app=order&act=ssapprove");
    	}
    

	 }
		
		
	//制单打印
    function ssprint() {
    	$ss_id = $_REQUEST['ss_id']?intval($_REQUEST['ss_id']):'0';
    	if(!$ss_id){
    		$this->show_warning('错误，查看制单出错','返回订单列表','index.php?app=order');
    		return;
    	}
    	$ss_info = $this->_ss_mod->get_info($ss_id);
    	$this->assign('ss_info',$ss_info);
    	$order=$this->_order_mod->get($ss_info['order_id']);
    	if(!$order) {
    		$this->show_warning('制单不存在');
    		return;
    	}
		
    	if ($order['brand_id']){
    		$this->_assign_main_label($order['brand_id']);
    		$this->_assign_listing($order['brand_id']);
    	}else{
    		$this->_assign_main_label();
    		$this->_assign_listing();
    	}
    	
		$order['company_name']=$this->getCompanyName($order['brand_id']);
		$user_info=$this->visitor->get();
		
		if($user_info['is_factory']==1&&$user_info['factory_id']!=$order['factory_id'])
		{
			$this->show_warning('无权限查看此制单','返回订单列表',"index.php?app=ss&act=order");
    		return;
		}
		
    	$this->assign('order',$order);
    	$fabric_info_mod =& m('fabric_info');
		$fabric_info_list = $fabric_info_mod->find(array(
		'conditions'=>'ss_id='.$ss_id,
		'fields'=>"fabric_info.fabric_info_id,ss_id,fabric_info.description,fabric_info.fabric_shop_id,item,price,width,picture,fabric_shop_name,fabric_characteristics,cc_id",
		'join'	=>"belongs_fabric_shop",
		'order'=>'fabric_info_id ASC'));
		$this->assign('fabric_info_list', $fabric_info_list);
    	$cq_mod =& m('cq');
    	$cq_info = $cq_mod->find(array("conditions"=>"ss_id=".$ss_id));
    	$this->assign('cq_info',$cq_info);
    	$size_ss_mod =& m('size_ss');
    	$size_ss_info = $size_ss_mod->find(array("conditions"=>"ss_id=".$ss_id,'order'=>'sort_order asc'));
		$this->_filter_size($size_ss_info);
    	$this->assign('size_ss_info',$size_ss_info);
		$this->assign('is_print',1);
		
		//挂衣绳位置列表
		$sling_mod=&m('sling');
		$sling_list=$sling_mod->find(array(
				'conditions'=>'if_show=1',
				'order'=>'sling_id desc'
		));
		$this->assign('sling_list',$sling_list);
		
		//备用扣位置列表
		$spare_mod=&m('spare');
		$spare_list=$spare_mod->find(array(
				'conditions'=>'if_show=1',
				'order'=>'spare_id desc'
		));
		$this->assign('spare_list',$spare_list);
		
		//洗水唛位置列表
		$water_mark_mod=&m('water_mark');
		$water_list=$water_mark_mod->find(array(
				'conditions'=>'if_show=1',
				'order'=>'water_id desc'
		));
		$this->assign('water_list',$water_list);
		 
		if($this->visitor->get('role')==USER_MERCHANDISER){
			$this->assign('is_edit',true);
		}
		
		//洗水唛附件
		$file_mod=&m('upload');
		$ss_waters=$file_mod->find(array(
				'conditions'=>'file_type='.BELONGS_SS_WATER_IMG.' and key_id='.$ss_id,
				'order'=>'file_id desc'
		));
		$this->assign('ss_waters',$ss_waters);
		
		//包装要求位置列表
		$package_mod=&m('package_demand');
		if ($ss_info['pack_id']){
			$pack_arr_id=explode(',', $ss_info['pack_id']);
			$con=" and pack_id not ".db_create_in($pack_arr_id);
			$pack_list=$package_mod->find(array(
					'conditions'=>'if_show=1 and pack_id '.db_create_in($pack_arr_id),
					'order'=>'pack_id desc'
			));
			$this->assign('pack_list',$pack_list);
		}
		$demand_list=$package_mod->find(array(
				'conditions'=>'if_show=1'.$con,
				'order'=>'pack_id desc'
		));
		$this->assign('demand_list',$demand_list);
		
		//赋值颜色数量类型
		$this->_assign_sizename($this,$ss_info['cq_sn_id'],$ss_info['size_sn_id']);
    	$this->display('ss.index.html');
    }
	
	
	//制单送审核时发送邮件
	function _send_mail_on_send_approve($order)
	{
		if(empty($order))
		{
			return false;
		}
		$user_mod =& m('member');
		
		$manger_list=$user_mod->find(array(
			"conditions"=>"role=".USER_MANAGER
			));
        $mail = get_mail('ss_on_send_approve', array('order' => $order));
	    foreach($manger_list as $item)
		{
			if(!empty($item['email']))
			{
			$this->_mailto($item['email'], addslashes($mail['subject']), addslashes($mail['message']));
			}
		 }
		 return true;

	}
	
		//审批不通过时发送邮件
	function _send_mail_on_approve_not_pass($order)
	{
		if(empty($order))
		{
			return false;
		}
		$user_mod =& m('member');
		$factory_id=!empty($order['factory_id'])?$order['factory_id']:"0";
		$factory_info=$user_mod->get("factory_id={$factory_id}");
		$merchandiser_id=!empty($order['merchandiser_id'])?$order['merchandiser_id']:"0";
		$merchandiser_info=$user_mod->get("user_id={$merchandiser_id}");
		
		$mail = get_mail('ss_on_approve_not_pass', array('order' => $order));
// 		 if(!empty($factory_info)&&!empty($factory_info['email']))
//		 {
//        	  $this->_mailto($factory_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
//			  
//		 }
		 if(!empty($merchandiser_info)&&!empty($merchandiser_info['email']))
		 {
        	  $this->_mailto($merchandiser_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
			   return true;
		 }
		 return true;

	}
	
	//审批通过待工厂确认时发送邮件
	function _send_mail_on_approve_pass($order)
	{
		if(empty($order))
		{
			return false;
		}
		$user_mod =& m('member');
		$factory_id=!empty($order['factory_id'])?$order['factory_id']:"0";
		$factory_info=$user_mod->get("factory_id={$factory_id}");
		$merchandiser_id=!empty($order['merchandiser_id'])?$order['merchandiser_id']:"0";
		$merchandiser_info=$user_mod->get("user_id={$merchandiser_id}");
		
		$mail = get_mail('ss_on_approve_pass', array('order' => $order));
 		 if(!empty($factory_info)&&!empty($factory_info['email']))
		 {
        	  $this->_mailto($factory_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
			  
		 }
		 if(!empty($merchandiser_info)&&!empty($merchandiser_info['email']))
		 {
        	  $this->_mailto($merchandiser_info['email'], addslashes($mail['subject']), addslashes($mail['message']));
			   return true;
		 }
		 
		 $qc_list=$user_mod->find(array(
			"conditions"=>"role=".USER_QC
			));
		 foreach($qc_list as $item)
		{
			if(!empty($item['email']))
			{
			$this->_mailto($item['email'], addslashes($mail['subject']), addslashes($mail['message']));
			}
		 }
		 return true;

	}
	
	function approve_not_pass()
	{
	 $this->display('ss.form_approve_not_pass.html');
	}
	
	static function _assign_sizename($obj_app,$cq_sn_id=NULL,$size_sn_id=NULL)
	{
		//赋值颜色数量尺寸名称列表
		$sizename_mod=&m("sizename");
		$sizename_list=$sizename_mod->find(array(
		'conditions'=>'',
		'order'=>'snid'
		));
		$obj_app->assign('sizename_list',$sizename_list);
		$obj_app->assign('sizename_list_json',ecm_json_encode($sizename_list));
		$default_sizename=current($sizename_list);
		$curr_cq_sizename=!empty($cq_sn_id)?$sizename_list[$cq_sn_id]:$default_sizename;
		$curr_size_sizename=!empty($size_sn_id)?$sizename_list[$size_sn_id]:$default_sizename;
		
		$obj_app->assign('curr_cq_sizename',$curr_cq_sizename);
		$obj_app->assign('curr_size_sizename',$curr_size_sizename);
	}
	
	//制单重置
	function resetss()
	{
		 $ss_id = $_REQUEST['ss_id']?intval($_REQUEST['ss_id']):'0';
    	if(!$ss_id){
    		$this->show_warning('错误，重置出错','返回订单列表','index.php?app=order');
    		return;
    	}
		$ss_info = $this->_ss_mod->get_info($ss_id);
			
		if(!in_array($this->visitor->get("role"), array(USER_MANAGER,USER_MERCHANDISER_ADMIN))){
    		$this->show_warning('错误，无权限重置此制单','返回订单列表','index.php?app=order');
    		return;
    	}
			$ss_data['ss_status']=SS_STATUS_CREATE;
		
    		$this->_ss_mod->edit($ss_id,$ss_data);
			//当审批通过和不通过时发送邮件
			$mod_pro_records=&m("pro_records");
			$log_time=gmtime();

			  $pro_data=array(
					"type"=>PRO_RECORDS_ORDER,
					"key_id"=>$ss_info["order_id"],
					"rec_name"=>$this->visitor->get("user_name"),
					"rec_buff"=>"重置制单",
					"rec_time"=>$log_time			
				);
				if(!$mod_pro_records->add($pro_data))
				{
				   $this->show_warning($mod_pro_records->get_error());
				}

    		$this->show_message('重置制单成功！',"查看制单","index.php?app=ss&act=index&ss_id={$ss_id}");

    	}
	
	function getCompanyName($brand_id)
	{
		if(!$brand_id){return false;}
		$mod_brand=&m("brand");
		$brand_info=$mod_brand->get(array(
		'conditions'=>"brand_id={$brand_id}",
		'join'		=>"belong_company"
		));
		
		return $brand_info["company_name"];
	}
	
	
	function _filter_size(&$size_ss_info)
	{
		foreach($size_ss_info as $key=>$item)
		{
			
			foreach($item as $key1=>$item1)
			{
				
				if(in_array($key1,array("w6","w8","w10","w12","w14","w16","code")))
				{
					
					if($item1==0)
					{
						$size_ss_info[$key][$key1]="";
					}
				}
			}
		}
	}
	
	
	//主唛列表
	function _assign_main_label($brand_id=null){
		$main_mod=&m('main_label');
		if ($brand_id){
			$main_list=$main_mod->find(array(
					'conditions'=>'brand_id='.$brand_id
			));
		}else{
			$main_list=$main_mod->find(array(
					'order'=>'m_id desc'
			));
		}
		$this->assign('main_list',$main_list);
	}
	
	//主唛、主唛图片联动
	function ajax_mian_label_img(){
		$main_file=array();
		$m_id=isset($_GET['m_id'])?$_GET['m_id']:0;
		if ($m_id){
			$file_mod=&m('upload');
			$main_file=$file_mod->get(array(
					'conditions' => 'file_type = ' . BELONGS_MAIN_LABEL . ' AND key_id = ' . $m_id,
					'order' => 'file_id ASC',
			));
		}
		exit(json_encode($main_file));
	}
	
	
	//挂牌列表
	function _assign_listing($brand_id=null){
		$listing_mod=&m('listing');
		if ($brand_id){
			$listing=$listing_mod->find(array(
					'conditions'=>'brand_id='.$brand_id
			));
		}else{
			$listing=$listing_mod->find(array(
					'order'=>'listing_time desc'
			));
		}
		$this->assign('listing',$listing);
	}
	
	//主唛、主唛图片联动
	function ajax_listing_img(){
		$listing_file=array();
		$l_id=isset($_GET['l_id'])?$_GET['l_id']:0;
		if ($l_id){
			$file_mod=&m('upload');
			$listing_file=$file_mod->get(array(
					'conditions' => 'file_type = ' . BELONGS_LISTING_IMG . ' AND key_id = ' . $l_id,
					'order' => 'file_id ASC',
			));
		}
		exit(json_encode($listing_file));
	}
}
?>
